﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Openapi.Weibo
{
    /// <summary>
    /// 微博工具类
    /// </summary>
    public class Utility
    {
        /// <summary>
        /// 将微博时间转换为DateTime
        /// </summary>
        /// <param name="dateString">微博时间字符串</param>
        /// <returns>DateTime</returns>
        public static DateTime ParseUTCDate(string dateString)
        {
            System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;

            DateTime dt = DateTime.ParseExact(dateString, "ddd MMM dd HH:mm:ss zzz yyyy", provider);

            return dt;
        }

        public static string BuildQueryString(IDictionary<string, string> parameters)
        {
            var value = parameters.Where(m=>!String.IsNullOrEmpty(m.Value)).Select(m =>
            {
                return String.Format("{0}={1}", Uri.EscapeDataString(m.Key), Uri.EscapeDataString(m.Value));
            }).ToArray();

            return String.Join("&",value);
        }

        /*
        internal static string BuildQueryString(params WeiboParameter[] parameters)
        {
            List<string> pairs = new List<string>();
            foreach (var item in parameters)
            {
                if (item.IsBinaryData)
                    continue;

                var value = string.Format("{0}", item.Value);
                if (!string.IsNullOrEmpty(value))
                {
                    pairs.Add(string.Format("{0}={1}", Uri.EscapeDataString(item.Name), Uri.EscapeDataString(value)));
                }
            }

            return string.Join("&", pairs.ToArray());
        }
        */

        /*
        public static string GetBoundary()
        {
            string pattern = "abcdefghijklmnopqrstuvwxyz0123456789";
            StringBuilder boundaryBuilder = new StringBuilder();
            Random rnd = new Random();
            for (int i = 0; i < 10; i++)
            {
                var index = rnd.Next(pattern.Length);
                boundaryBuilder.Append(pattern[index]);
            }
            return boundaryBuilder.ToString();
        }

        /// <summary>
        /// 生成Json格式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson<T>(T obj)
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                json.WriteObject(stream, obj);
                string szJson = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                return szJson;
            }
        }
        /// <summary>
        /// 将JSON 字符转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string str)
        {
            T obj = Activator.CreateInstance<T>();
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }
        */
    }

}
